package com.wjj.service;

import com.wjj.dao.repository.ClassRepository;
import com.wjj.dto.ClassDTO;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description
 * @Author junjun Wang
 * @Date Create in 20:23 2023/12/7
 **/
@Service
public class ClassServiceImpl implements ClassService {

	@Autowired
	private ClassRepository classRepository;

	@Override
	public List<ClassDTO> getClasses() {
		return classRepository.getClasses().stream()
				.map(ClassMapper::fromClassPO)
				.collect(Collectors.toList());
	}

	@Override
	public ClassDTO getClassById(Integer classId) {
		return ClassMapper.fromClassPO(classRepository.getClassById(classId));
	}

	@Override
	@Transactional
	public Boolean addClass(List<ClassDTO> classes) {
		return classRepository.addClass(classes.stream()
				.map(ClassMapper::toClassPO).collect(Collectors.toList()));
	}

	@Override
	@Transactional
	public Integer updateClass(ClassDTO classDTO) {
		return classRepository.updateClass(ClassMapper.toClassPO(classDTO));
	}

	@Override
	@Transactional
	public Integer deleteClassById(Integer classId) {
		return classRepository.deleteClassById(classId);
	}
}
